package net.seehope.foodie.controller;

import java.lang.reflect.InvocationTargetException;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.seehope.foodie.common.JsonResult;
import net.seehope.foodie.pojo.bo.AddAddressBo;
import net.seehope.foodie.pojo.bo.UpdateBo;
import net.seehope.foodie.service.AddressService;

@Api("收货地址相关的API")
@RestController
@RequestMapping("/address")
public class AddressController {
	
	@Autowired
	private AddressService addressService;
	
	
	@ApiOperation("展示地址列表")
	@PostMapping("/list")
	public JsonResult showAddressList(String userId) {
		
		return JsonResult.isOk(addressService.showAddressList(userId));
	}
	
	
	@ApiOperation("设置默认地址")
	@PostMapping("/setDefalut")
	public JsonResult setDefaultAddress(String userId,String addressId) {
		
		addressService.setDefaultAddress(userId, addressId);
		return JsonResult.isOk(null);
		
	}
	
	
	@ApiOperation("编辑地址信息")
	@PostMapping("/update")
	public JsonResult update(@RequestBody UpdateBo bo) {
		addressService.updateAddress(bo);
		return JsonResult.isOk(null);
		
	}
	
	
	@ApiOperation("删除地址信息")
	@PostMapping("/delete")
	public JsonResult delete(String userId,String addressId) {
		addressService.deleteAddress(userId, addressId);
		return JsonResult.isOk(null);
	}
	
	@ApiOperation("增加地址信息")
	@PostMapping("/add")
	public JsonResult add(@Valid @RequestBody AddAddressBo bo) throws IllegalAccessException, InvocationTargetException {
		addressService.addAddress(bo);
		return JsonResult.isOk(null);
		
	}
	
	

}
